package com.lagou.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.lagou.biz.PositionBiz;
import com.lagou.biz.impl.PositionBizImpl;
import com.lagou.dao.UBasicDao;
import com.lagou.entity.Position;
import com.lagou.entity.UBasic;
import com.lagou.entity.User;
import com.lagou.util.JDBCUtil2;


public class UBasicDaoImpl implements UBasicDao{
	JDBCUtil2 util=JDBCUtil2.getInit();
	PositionBiz pb = new PositionBizImpl();
	@Override
	public boolean saveUBasic(UBasic ubasic) {
		String sql="insert into t_ubasic(u_id,u_name,u_gender,u_education,workexperience,phone,photo,usn) values (?,?,?,?,?,?,?,?)";
		return util.update(sql, ubasic.getUser().getUid(),ubasic.getUname(),ubasic.getUgender(),ubasic.getUeducation(),ubasic.getWorkexperience(),ubasic.getPhone(),ubasic.getPhoto(),ubasic.getUsn());
	}

	@Override
	public boolean upadateBasic(UBasic ubasic) {
		String sql = "update t_ubasic set u_name=?,u_gender=?,u_education=?,workexperience=?,phone=?,photo=?,usn=? where ubid=?";
		return util.update(sql, ubasic.getUname(),ubasic.getUgender(),ubasic.getUeducation(),ubasic.getWorkexperience(),ubasic.getPhone(),ubasic.getPhoto(),ubasic.getUsn(),ubasic.getUbid());
	}


	@Override
	public List<Position> qureyAllCollect(int ubid) {
		CachedRowSet rs=util.query("select * from t_collection WHERE ubid = ?",ubid);
		List<Position> collection = new ArrayList<Position>();
		try {
			while(rs.next()) {
				int pid = rs.getInt("pid");
				Position p = pb.queryAllByID(pid);
//				System.out.println(p);
				collection.add(p);				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return collection;	
	}

	@Override
	public boolean addCPosition(int pid, int ubid) {
		return util.update("insert into t_collection (pid,ubid) value(?,?)", pid,ubid);
	}

	@Override
	public boolean deleteCPosition(int pid, int ubid) {
		return util.update("delete from t_collection where ubid=? and pid=?",ubid,pid);
	}


	@Override
	public UBasic queryByUid(int uid) {
		UBasic ub=null;
		CachedRowSet rs=util.query("select * from t_ubasic WHERE u_id = ?",uid);
		try {
			if(rs.next()) {
		     ub = new UBasic();
		    ub.setUbid(rs.getInt("ubid"));
		    User u = new User();
		    u.setUid(rs.getInt("u_id"));
		    ub.setUser(u);
		    ub.setUname(rs.getString("u_name"));
		    ub.setUgender(rs.getString("u_gender"));
		    ub.setUeducation(rs.getString("u_education"));
		    ub.setWorkexperience(rs.getString("workexperience"));
		    ub.setPhone(rs.getString("phone"));
		    ub.setPhoto(rs.getString("photo"));
		    ub.setUsn(rs.getString("usn"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return ub;
	}

	@Override
	public boolean addUBasic(int uid) {
		String sql="INSERT INTO t_ubasic (u_id,u_name,u_gender,u_education,workexperience,u_status,phone,photo,usn) VALUES(?,?,?,?,?,?,?,?,?)";
		return util.update(sql, uid,"","","","",1,"","","");
	}

	@Override
	public User queryByUbid(int uid) {
		User u = new User();
		CachedRowSet rs=util.query("select * from t_user WHERE u_id = ?",uid);
		try {
			if(rs.next()) {
		    u.setUemail(rs.getString("u_email"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return u;
	}

	@Override
	public UBasic queryUByUbid(int ubid) {
		UBasic ub=null;
		CachedRowSet rs=util.query("select * from t_ubasic WHERE ubid = ?",ubid);
		try {
			if(rs.next()) {
		    ub = new UBasic();
		    ub.setUbid(rs.getInt("ubid"));
		    User u = new User();
		    u.setUid(rs.getInt("u_id"));
		    ub.setUser(u);
		    ub.setUname(rs.getString("u_name"));
		    ub.setUgender(rs.getString("u_gender"));
		    ub.setUeducation(rs.getString("u_education"));
		    ub.setWorkexperience(rs.getString("workexperience"));
		    ub.setPhone(rs.getString("phone"));
		    ub.setPhoto(rs.getString("photo"));
		    ub.setUsn(rs.getString("usn"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return ub;
	}

	@Override
	public int queryCCount() {
		String sql = "select count(jilu) jilu from t_collection";
		CachedRowSet rs=util.query(sql);
		try {
			if(rs.next()) {
				return rs.getInt("jilu");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return -1;
	}

	@Override
	public List<Position> qureyByPage(int page, int size,int ubid) {
		String sql ="select * from t_collection  WHERE ubid = ? ORDER BY jilu DESC LIMIT "+(page-1)*size+" , "+size;
		CachedRowSet rs=util.query(sql,ubid);
		List<Position> collection = new ArrayList<Position>();
		try {
			while(rs.next()) {
				int pid = rs.getInt("pid");
				Position p = pb.queryAllByID(pid);
//				System.out.println(p);
				collection.add(p);				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return collection;	
	}
	
}
